package com.library.workbench.web.controller.admin;

import com.library.commons.constant.Constant;
import com.library.commons.domain.ReturnObject;
import com.library.commons.utils.UUIDUtils;
import com.library.workbench.pojo.Book;
import com.library.workbench.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/workbench/admin/book/toDetail.do")
    public String toDetail(){
        return "workbench/admin/book/detail";
    }

    @RequestMapping("/workbench/admin/queryAllBooksForPage.do")
    @ResponseBody
    public Object queryAllBooksForPage(String bookName,String author,String publisher,Integer pageNo,Integer pageSize){
        // 封装对象
        Map<String,Object> map=new HashMap<>();
        map.put("bookName",bookName);
        map.put("author",author);
        map.put("publisher",publisher);
        map.put("beginNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        // 调用service层方法
        List<Book> books = bookService.queryBookByConditionForPage(map);
        int rowsSum = bookService.queryRowsSum(map);
        Map<String,Object> res=new HashMap<>();
        map.put("books",books);
        map.put("totalRows",rowsSum);
        // 返回信息
        return map;
    }

    @RequestMapping("/workbench/admin/book/saveCreateBook.do")
    @ResponseBody
    public Object saveCreateBook(Book book){
        // 封装参数
        book.setBookId(UUIDUtils.getUUID());
        book.setExist("1");
        ReturnObject returnObject=new ReturnObject();
        try {
            // 调用service层方法
            int res = bookService.saveCreateBook(book);
            if (res>0){
                returnObject.setCode(Constant.RETURN_SUCCESS);
            }else {
                returnObject.setMessage(Constant.RETURN_FAIL);
                returnObject.setMessage("系统繁忙，请稍候再试....");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setMessage(Constant.RETURN_FAIL);
            returnObject.setMessage("系统繁忙，请稍候再试....");
        }
        return returnObject;
    }

    @RequestMapping("/workbench/admin/book/deleteBookByBookId.do")
    @ResponseBody
    public Object deleteBookByBookId(String bookId){
        ReturnObject returnObject=new ReturnObject();
        try {
            // 调用service层方法，删除书籍
            int res = bookService.deleteBookByBookId(bookId);
            if (res>0){
                returnObject.setCode(Constant.RETURN_SUCCESS);
            }else {
                returnObject.setCode(Constant.RETURN_FAIL);
                returnObject.setMessage("系统繁忙，请稍候再试....");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("系统繁忙，请稍候再试....");
        }
        return returnObject;
    }

    @RequestMapping("/workbench/admin/book/queryBookInformationByBookId.do")
    @ResponseBody
    public Object queryBookInformationByBookId(String bookId){
        // 调用service层方法查询书籍信息
        Book book = bookService.queryBookByBookId(bookId);
        // 生成响应信息
        return book;
    }

    @RequestMapping("/workbench/admin/book/updateBookByBookId.do")
    @ResponseBody
    public Object updateBookByBookId(Book book){
        ReturnObject returnObject=new ReturnObject();
        try {
            // 调用service层方法，更新书籍信息
            int res = bookService.updateBookByBookId(book);
            if (res>0){
                returnObject.setCode(Constant.RETURN_SUCCESS);
            }else {
                returnObject.setCode(Constant.RETURN_FAIL);
                returnObject.setMessage("系统繁忙，请稍候再试....");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("系统繁忙，请稍候再试....");
        }
        return returnObject;
    }
}
